package test;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.dao.TSupplierMapper;
import cn.cvs.pojo.TSupplier;
import cn.cvs.pojo.TSysUser;
import cn.cvs.utils.MybatsiUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Ch02Test_User {

    @Test
    public  void testGetUsersByRealName_ch01(){
        try {
            SqlSession session= MybatsiUtil.createSession();
            String realName ="李";
            String userName ="吴";
            List<TSysUser> list= session.getMapper(SysUserMapper.class).getUsersByRealName(realName);
            list.forEach(user -> System.out.println(user));
            //System.out.println(list.size());
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            MybatsiUtil.closeSession();
        }

    }
    @Test
    public  void testGetUsersByRealName_sl2_sl3_sl13(){
        try {
            SqlSession session= MybatsiUtil.createSession();
            //构建参数
            TSysUser user =new TSysUser();
            user.setRealName("李");
            user.setRoleId(3);

            Map<String,Object> maps = new HashMap<>();
            maps.put("rid",2);
            maps.put("hhl","赵");

            //执行查询
            //参数
            List<TSysUser> list= session.getMapper(SysUserMapper.class).getUsersByPojo(user);
//            List<TSysUser> list= session.getMapper(SysUserMapper.class).getUsersByMap(maps);//.getUsersByPojo(user);
//            List<TSysUser> list= session.getMapper(SysUserMapper.class).getUserListWithRoleName(user);

            //示例13 查询结果映射
           // List<TSysUser> list= session.getMapper(SysUserMapper.class).getUserListWithRoleName(user);
            list.forEach(u -> System.out.println(u));
            //System.out.println(list.size());
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            MybatsiUtil.closeSession();
        }

    }
    //getUserListByRoleId

    @Test
    public  void testGetUsersByRealName_sl16(){
        try {
            SqlSession session= MybatsiUtil.createSession();

            //示例16 嵌套结果映射
            List<TSysUser> list= session.getMapper(SysUserMapper.class).getUserListByRoleId(2);
            list.forEach(u -> System.out.println(u));
            //System.out.println(list.size());
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            MybatsiUtil.closeSession();
        }

    }

    @Test
    public  void testGetUsersByRealName_sl17(){
        try {
            SqlSession session= MybatsiUtil.createSession();

            //示例16 嵌套结果映射
            List<TSysUser> list= session.getMapper(SysUserMapper.class).getUserAndAddressByUserId(1);
            if (null!=list){
                for (TSysUser user:list){
                    System.out.println(user);
                    if (null !=user && null!=user.getAddressList()) {
                        user.getAddressList().forEach(addr-> System.out.println(addr));
                    }
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            MybatsiUtil.closeSession();
        }

    }
}
